package com.example.serviceproduct.controller;

import com.example.serviceproduct.entity.Cart;
import com.example.serviceproduct.entity.CartItem;
import com.example.serviceproduct.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;

/**
 * 购物车控制器
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    private final CartService cartService;

    @Autowired
    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    /** 获取用户购物车 */
    @GetMapping("/{userId}")
    public Cart getCart(@PathVariable Long userId) {
        return cartService.getCartByUserId(userId);
    }

    /** 获取购物车所有项 */
    @GetMapping("/{userId}/items")
    public List<CartItem> getCartItems(@PathVariable Long userId) {
        return cartService.getCartItems(userId);
    }

    /** 添加商品到购物车 */
    @PostMapping("/{userId}/add")
    public Cart addItem(@PathVariable Long userId, @RequestParam Long productId, @RequestParam Integer quantity) {
        return cartService.addItemToCart(userId, productId, quantity);
    }

    /** 更新购物车中商品数量 */
    @PostMapping("/{userId}/update")
    public Cart updateItem(@PathVariable Long userId, @RequestParam Long productId, @RequestParam Integer quantity) {
        return cartService.updateItemQuantity(userId, productId, quantity);
    }

    /** 从购物车移除商品 */
    @PostMapping("/{userId}/remove")
    public Cart removeItem(@PathVariable Long userId, @RequestParam Long productId) {
        return cartService.removeItemFromCart(userId, productId);
    }

    /** 清空购物车 */
    @PostMapping("/{userId}/clear")
    public void clearCart(@PathVariable Long userId) {
        cartService.clearCart(userId);
    }

    /**
     * 生成订单号
     *
     * @return 订单号
     */
    @GetMapping("/generate-order-no")
    public String generateOrderNo() {
        // 生成唯一的订单号
        return "P" + UUID.randomUUID().toString().replace("-", "");
    }
} 